/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cutils/memory.h>

#include <utils/Log.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>

#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>

using namespace android;

#define CHECK_RET() do{ if(ret) \
printf("error:\t\tline:%d ret:%d\n", __LINE__, ret); \
else \
	printf("OK:\t\tline:%d ret:%d\n", __LINE__, ret); \
} while(0)

EGLDisplay mEglDisplay;
EGLSurface mEglSurface;
EGLContext mEglContext;
static EGLint sDefaultConfigAttribs[] = {
	EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
	EGL_NONE
};
void init_egl(){
	 // We need a valid GL context so we can test updateTexImage()
    		// This initializes EGL and create a dummy GL context with a
    		// pbuffer render target.
	mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);

	EGLint majorVersion, minorVersion;
	eglInitialize(mEglDisplay, &majorVersion, &minorVersion);

	EGLConfig myConfig;
	EGLint numConfigs = 0;
	eglChooseConfig(mEglDisplay, sDefaultConfigAttribs,
		&myConfig, 1, &numConfigs);

	EGLint pbufferAttribs[] = {
		EGL_WIDTH, 16,
		EGL_HEIGHT, 16,
		EGL_NONE };
		mEglSurface = eglCreatePbufferSurface(mEglDisplay, myConfig, pbufferAttribs);
		mEglContext = eglCreateContext(mEglDisplay, myConfig, EGL_NO_CONTEXT, 0);
		eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext);
	}

	int main(int argc, char** argv)
	{
		int ret = 0;
		int width = 1920, height = 1080;
		printf("entry main\n");

		init_egl();

		sp<SurfaceTexture> mST;
		sp<SurfaceTextureClient> mSTC;
		sp<ANativeWindow> mANW;
		sp<ISurfaceTexture> mIST;


		mST = new SurfaceTexture(123);
		mSTC = new SurfaceTextureClient(mST);
		mANW = mSTC;
		mIST = mSTC->getISurfaceTexture();

		ret = mST->setDefaultBufferSize(width, height);
		CHECK_RET();
		ret = mST->setDefaultMaxBufferCount(3);
		CHECK_RET();    

		sp<GraphicBuffer> buf;
		int slot;
		sp<Fence> fence;

		ret = mIST->dequeueBuffer(&slot, fence, width, height, PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_WRITE_OFTEN);
		CHECK_RET();

		uint8_t* img = NULL;
		for(int i=0; i<5; i++){
			ret = mIST->requestBuffer(slot, &buf);
			CHECK_RET();    
			ret = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
			CHECK_RET();
			printf("buffer address:%d\n", img);
			memset(img, 128, width*height*4);
			buf->unlock();

			
			mST->updateTexImage();
		}

		int64_t timestamp;
		timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
		Rect crop(width, height);
		ISurfaceTexture::QueueBufferOutput output;
		ISurfaceTexture::QueueBufferInput input(timestamp, crop, NATIVE_WINDOW_SCALING_MODE_FREEZE,
			0, fence);
		ret = mIST->queueBuffer(slot, input, &output);
		CHECK_RET();
		

		printf("test complete. CTRL+C to finish.\n");

		sleep(5);
		return 0;
	}
